/**
 * Created by chenyong on 2015/3/21.
 */

function pano()
{
    return document.getElementById("panoSWFObject");
}

function pano_freeze()
{
    pano().call("freezeview(true);");
}

function pano_unfreeze()
{
    pano().call("freezeview(false);");
}

function pano_get_viewposition(){

    var krpano = document.getElementById("panoSWFObject");
    var fov = Number( krpano.get("view.fov") );
    var v = Number( krpano.get("view.vlookat") );
    var h   = Number( krpano.get("view.hlookat") );
    h = h%360; // restrict to +-360
    if (h<=-180){
        h = 360+h;
    }
    else if(h>180){
        h = 360-h;
    }

    pos = {'fov':fov,'ath':h,'atv':v};
    return pos;
}


var prev_click = 0;
var dblclick_timeout = 500;
function pano_dblclick(func)
{
    date = new Date();
    now_click = date.getTime();

    if(now_click < (prev_click+dblclick_timeout)){
        prev_click = 0;
        eval(func+'()');
    }else{
        prev_click = now_click;
    }
}

function pano_screentosphere(coords)
{
    var panoobj = pano();

    if (panoobj && panoobj.get && coords)
    {
        hvs = panoobj.get("screentosphere("+coords.x +","+coords.y +")");
        hva = hvs.split(",");

        return {'ath': Number( hva[0] ), 'atv': Number( hva[1] )};
    }
    return false;
}

function pano_getCoords()
{
    var panoobj = pano();

    if (panoobj && panoobj.get)
    {
        x = panoobj.get("mouse.x");
        y = panoobj.get("mouse.y");
        z = panoobj.get("view.fov");

        return {'x':x,'y':y,'z':z};
    }

    return false;
}

function pano_zoom()
{
    var panoobj = pano();

    if (panoobj && panoobj.get && double_click)
    {
        coords = pano_getCoords();
        if (coords)
        {
            fovmin = panoobj.get("view.fovmin");
            fov = coords.z / 2;
            if(fov < fovmin){
                fov = fovmin;
            }
            spherecoords = pano_screentosphere(coords);
            if(spherecoords && fov){
                panoobj.call("lookto( " + spherecoords.ath + ", " + spherecoords.atv + ", "+ fov +",smooth(720,-720,720));");
            }
        }
    }
}

function lookto(atv, ath, fov){
    pano().call("lookto( get(view.hlookat), get(view.vlookat), 60,tween(easeOutQuad,2));");
    pano().call("lookto( " + ath + ", " + atv + ", 60,tween(easeOutQuad,2));");
    pano().call("lookto( " + ath + ", " + atv + ", "+ fov +",tween(easeOutQuad,2));");
}

function pano_callcb(panocb){

    if($.bigpixel.debug()){ console.log( 'pano_callcb :: ' + panocb); }

    var action = "" + pano().get(panocb);
    if($.bigpixel.debug()){ console.log( 'pano_callcb :: ' +  action);}

    pano().call(action);
}